package com.yy.jhl.ui.manager.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.JavascriptInterface;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.yy.common.base.BaseActivity;
import com.yy.common.commonutils.SPUtils;
import com.yy.common.commonutils.StringUtil;
import com.yy.common.commonwidget.OnNoDoubleClickListener;
import com.yy.jhl.R;
import com.yy.jhl.app.AppConstant;
import com.yy.jhl.app.MyApplication;
import com.yy.jhl.bean.ProductInfo;
import com.yy.jhl.ui.manager.adapter.ProductAdapter;
import com.yy.jhl.ui.manager.contract.ProductListContract;
import com.yy.jhl.ui.manager.presenter.ProductListPresenter;
import com.yy.jhl.util.DeviceUtil;

import java.io.Serializable;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by libingn on 2017/12/20.
 */

public class ProductListWebViewActivity extends BaseActivity {
    @BindView(R.id.webview)
    WebView webView;
    String orderId = "";
    String batchId = "";
    String productItems = "";
    public static void startAction(Context mContext, String orderId,String batchId,String productItems,int requestCode) {
        Intent intent = new Intent(mContext, ProductListWebViewActivity.class);
        intent.putExtra("orderId", orderId);
        intent.putExtra("batchId", batchId);
        intent.putExtra("productItems", productItems);
        ((Activity)mContext).startActivityForResult(intent,requestCode);
    }
    @Override
    public int getLayoutId() {
        return R.layout.act_product_list_webview;
    }

    @Override
    public void initView() {
        initWebView();
        batchId = getIntent().getStringExtra("batchId");
        orderId = getIntent().getStringExtra("orderId");
        productItems = getIntent().getStringExtra("productItems");
        showTitle("产品列表", new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                finish();
            }
        });
    }
    private void initWebView(){
        WebSettings settings = webView.getSettings();
        String userAgent = "Android--"+DeviceUtil.getPhoneBrand()+"--"+DeviceUtil.getPhoneModel();
        settings.setUserAgentString(userAgent);
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        settings.setJavaScriptEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
//        webView.setWebChromeClient(new WebChromeClient(){
//            @Override
//            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
//                showShortToast(message);
//                return true;
//            }
//        });
        //加载网页路径
        webView.addJavascriptInterface(new JsToJava(), "product");
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String urlConection) {
                view.loadUrl(urlConection);
                return super.shouldOverrideUrlLoading(view, urlConection);
            }
        });
        int version = SPUtils.getSharedIntData(mContext,AppConstant.SP_KEY_URL_VERSION_CHUKU)+1;
        SPUtils.setSharedIntData(mContext,AppConstant.SP_KEY_URL_VERSION_CHUKU,version);
        String baseUrl = getResources().getString(R.string.url_base);
        webView.loadUrl(baseUrl+"index.html?a="+version);
    }
    // 原生的方式
    public class JsToJava {
        // 高版本需要加这个注解才能生效
        @JavascriptInterface
        public String getOrderId() {
            return orderId;
        }
        @JavascriptInterface
        public String getToken() {
            String token = SPUtils.getSharedStringData(MyApplication.getAppContext(), AppConstant.SP_KEY_TOKEN);
            return token;
        }
        @JavascriptInterface
        public String getBatchId() {
            return batchId;
        }
        @JavascriptInterface
        public String getProductItems() {
            return productItems;
        }
        @JavascriptInterface
        public String getUserAgent() {
            String userAgent = "Android--"+DeviceUtil.getPhoneBrand()+"--"+DeviceUtil.getPhoneModel();
            return userAgent;
        }
        @JavascriptInterface
        public void alert(String message) {
            showShortToast(message);
        }
        @JavascriptInterface
        public void getProductList(String items) {
            Intent intent = new Intent();
            intent.putExtra("productList",items);
            setResult(RESULT_OK,intent);
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        //清空所有Cookie
        CookieSyncManager.createInstance(mContext);  //Create a singleton CookieSyncManager within a context
        CookieManager cookieManager = CookieManager.getInstance(); // the singleton CookieManager instance
        cookieManager.removeAllCookie();// Removes all cookies.
        CookieSyncManager.getInstance().sync(); // forces sync manager to sync now
        webView.setWebChromeClient(null);
        webView.setWebViewClient(null);
        webView.getSettings().setJavaScriptEnabled(false);
        webView.clearCache(true);
        super.onDestroy();
    }
}
